package com.ruoyi.project.system.wxcontroller;


import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.mapper.*;
import com.ruoyi.project.system.service.IWxApiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.rmi.CORBA.Util;
import java.util.*;

@RestController
@RequestMapping("/api")
public class WxApiController extends BaseController {

    @Autowired
    IWxApiService wxApiService;

    @Autowired
    CourseQuestionCfgMapper courseQuestionCfgMapper;


    @Autowired
    QuestionStoreMapper questionStoreMapper;

    @Autowired
    CourseCatalogMapper courseCatalogMapper;

    @Autowired
    CourseMapper courseMapper;
    @Autowired
    KnowledgeCatalogMapper knowledgeCatalogMapper;
    @Autowired
    KnowledgeArticleMapper knowledgeArticleMapper;


    @Autowired
    CollegePaperAnswerDetailMapper collegePaperAnswerDetailMapper;

    @Autowired
    CollegePaperAnswerMapper collegePaperAnswerMapper;

    /*  *//**
     * 首页获取课程下拉
     * @return
     *//*
    @GetMapping("/getCourseList")
    public AjaxResult getCourseList(){
        List<Map<String, Object>> list = wxApiService.getCourseList();
        return AjaxResult.success(list);
    }


    *//**
     * 首页获取安全知识
     * @return
     *//*
    @GetMapping("/getknowledgeCatalogList")
    public AjaxResult getknowledgeCatalogList(){
        List<Map<String, Object>> list = wxApiService.getknowledgeCatalogList();
        return AjaxResult.success(list);
    }*/



    /**
     * 首页获取课程下拉和具体课程内容
     * @return
     */
    @GetMapping("/getCourseList")
    public AjaxResult getCourseList(){
        //获取课程下拉
        CourseCatalog courseCatalog = new CourseCatalog();
        courseCatalog.setId(0);
        courseCatalog.setName("全部数据");
        //下拉数据结构
        List<CourseCatalog> courseSelectList = new ArrayList<>();
        courseSelectList.add(courseCatalog);
        List<CourseCatalog> courseCatalogs = courseCatalogMapper.selectCourseCatalogList(new CourseCatalog());
        courseSelectList.addAll(courseCatalogs);

        //具体课程内容
        List<Course> list = courseMapper.getCourseInfoList(new Course());
        Map<String, Object> map= new HashMap<>();
        map.put("select",courseSelectList);
        map.put("info",list);
        return AjaxResult.success(map);
    }


    /**
     * 首页获取安全知识下拉和具体信息
     * @return
     */
    @GetMapping("/getknowledgeCatalogList")
    public AjaxResult getknowledgeCatalogList(){
        //获取课程下拉

        KnowledgeCatalog knowledgeCatalog =new KnowledgeCatalog();
        knowledgeCatalog.setId(0);
        knowledgeCatalog.setName("全部数据");
        List<KnowledgeCatalog> courseSelectList = new ArrayList<>();
        courseSelectList.add(knowledgeCatalog);
        List<KnowledgeCatalog> courseList = knowledgeCatalogMapper.selectWxKnowledgeCatalogList(new KnowledgeCatalog());
        courseSelectList.addAll(courseList);
        //具体课程内容
        List<KnowledgeArticle> list = knowledgeArticleMapper.getKaInfoList(new KnowledgeArticle());
        Map<String, Object> map= new HashMap<>();
        map.put("select",courseSelectList);
        map.put("info",list);
        return AjaxResult.success(map);
    }


    /**
     * 课程下拉搜索
     */

    @GetMapping("/getCourseIdList")
    public AjaxResult getCourseIdList(Integer id){

        //具体课程内容
        Course course = new Course();
        if(id!=0){
            course.setCourseCatalogId(id.longValue());
        }
        List<Course> list = courseMapper.getCourseInfoList(course);
        return AjaxResult.success(list);
    }


    /**
     * 安全知识下拉搜索
     * @return
     */
    @GetMapping("/getknowledgeCatalogIdList")
    public AjaxResult getknowledgeCatalogIdList(Integer id){
        //获取安全知识
        KnowledgeArticle knowledgeArticle = new KnowledgeArticle();
        if(id!=0){
            knowledgeArticle.setCatalogTreeId(id+"");
        }
        List<KnowledgeArticle> list = knowledgeArticleMapper.getKaInfoList(knowledgeArticle);

        return AjaxResult.success(list);
    }

    /**
     * 获取习题
     * @param courseCatalogId
     * @return
     */
    @GetMapping("/selectQuestionStoreByCourseCatalogId")
    public AjaxResult selectQuestionStoreByCourseCatalogId(Long courseCatalogId){
        List<QuestionStore> list = new ArrayList<>();


        List<CourseQuestionCfg> courseQuestionCfg = courseQuestionCfgMapper.selectCourseQuestionCfgListByquId(courseCatalogId);

        if(courseQuestionCfg.size()!=0){
            for(int i = 0;i<3;i++){
                list.add(questionStoreMapper.selectQuestionStoreById(courseQuestionCfg.get(i).getQuestionId().intValue()));
            }
            return AjaxResult.success(list);
        }

        return AjaxResult.success(list);

    }


    /**
     * 获取考试列表，若未进行考试  ksScore字段为1000 进行考试则是该实际分数
     * @return
     */
    @GetMapping("/getExamList")
    public AjaxResult getExamList(){

        List<Map<String,Object>> list = new ArrayList<>();

        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());

        if(loginUser.getStudent()!=null){

            list =  questionStoreMapper.getExamStudentList(loginUser.getStudent().getCollegeId());

            for (Map<String, Object> map : list) {
                Map<String,Object> answer = collegePaperAnswerMapper.getAnswer(loginUser.getStudent().getId(),Integer.parseInt(map.get("id")+""));
                if(answer==null){
                    map.put("ksScore",1000);
                }else{
                    map.put("ksScore",answer.get("score"));
                }
            }

        }else if(loginUser.getCollegeStaff()!=null){

            list =  questionStoreMapper.getExamList(loginUser.getCollegeStaff().getCollegeId().intValue());

        }

        return AjaxResult.success(list);

    }


    /**
     * //获取考试试卷列表
     * @return
     */
    @GetMapping("/getExamInfoList")
    public AjaxResult getExamInfoList(Integer id){

        List<Map<String,Object>> list = new ArrayList<>();

        list =  questionStoreMapper.getExamInfoList(id);

        return AjaxResult.success(list);

    }


    /**
     * //获取考试试卷结果列表
     * @return
     */
    @GetMapping("/getExamResultList")
    public AjaxResult getExamResultList(Integer id){

        Map<String,Object> map = new HashMap<>();

        List<Map<String,Object>> list = questionStoreMapper.getExamInfoList(id);


        List<Map<String,Object>> result =  collegePaperAnswerDetailMapper.getEcamResultList(id, SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest()).getStudent().getId());


        map.put("info",list);
        map.put("result",result);

        return AjaxResult.success(map);

    }



}
